// pages/shop/shop.js

var id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
  // goods:[],
  // domain: getApp().globalData.domain,
    goodsNum: 1,
    // zIndex:true,
    seriesId:'',
    seriesTitle:'',
    jsCode: ''
  },
  makePhone:function(){
    wx.makePhoneCall({
      phoneNumber: getApp().globalData.telephone //仅为示例，并非真实的电话号码
    });
  },
  shopCart: function () {
    wx.navigateTo({
      url: "../cart/cart"
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.goods();
    // console.log(options);
    // this.setData({
    //   id: options.id,
    //   name: options.name,
    //   img: options.img,
    //   introduce: options.introduce,
    //   sales_volume: options.sales_volume,
    //   stock: options.stock,
    //   price: options.price,
    // })
    this.loadShop(options.id);
    id = options.id;
    this.gselectShop(options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // shopSelect:function(){
  //   this.setData({
  //     zIndex:false
  //   })
  // },
  // backShop:function(){
  //   this.setData({
  //     zIndex: true
  //   })
  // },
  hotShop: function (id) {
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/commodity/hot/detail?commodity_id=' + id,
      data: {
        // id: id,
        // openid: getApp().globalData.openid,
        // token: getApp().globalData.userInfo.token
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(res)
        that.setData({
          id: res.data.id,
          name: res.data.name,
          phone_number: res.data.phone_number,
          region: res.data.region,
          detailed_address: res.data.detailed_address,
        })
      },
      fail: function (err) { },
      complete: function () { }
    });
  },
  loadShop: function (id) {
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/commodity/detail?commodity_id=' + id,
      data: {
        // id: id,
        // openid: getApp().globalData.openid,
        // token: getApp().globalData.userInfo.token
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(res)
        that.setData({
          id: res.data[0].id,
          name: res.data[0].name,
          price: res.data[0].price,
          stock: res.data[0].stock,
          sales_volume: res.data[0].sales_volume,
          img: getApp().globalData.domain + 'storage/' + res.data[0].img,
          introduce: res.data[0].introduce,
        })
      },
      fail: function (err) { },
      complete: function () { }
    });
  },
  gselectShop: function (id) {
    var that = this;
    wx.request({
      url: getApp().globalData.domain + 'api/CommoditySeries/list?commodity_id=' + id,
      data: {
        // id: id,
        // openid: getApp().globalData.openid,
        // token: getApp().globalData.userInfo.token
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        // console.log(id)
        // console.log(res)
        that.setData({
          select:res.data.data
          // id: res.data.data[0].id,
          // commodity_id: res.data.data[0].commodity_id,
          // title: res.data.data[0].title,
        })
      },
      fail: function (err) { },
      complete: function () { }
    });
  },
  seriesSelect:function(e){
    // console.log(e)
    var seriesid = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var title = this.data.select[index].title
    this.setHighlight(index)
    this.setData({ seriesId: seriesid, seriesTitle: title })
    // console.log(this.data.seriesId)
    // console.log(this.data.seriesTitle)
  },
  setHighlight: function (index) {
    var that = this
    var highlight = []
    for (var i = 0; i < that.data.select; i++) {
      highlight[i] = ''
    }
    highlight[index] = 'highlight'
    that.setData({ highlight: highlight })
  },
  bindMinus: function () {
    var num = this.data.goodsNum
    if (num > 1) {
      num--
    }
    this.setData({ goodsNum: num })
  },

  /**
   * 购买数量值
   */
  bindManual: function (e) {
    var num = e.detail.value
    if (num < 1) {
      num = 1
    }
    this.setData({ goodsNum: num })
  },

  /**
   * 购买数量加
   */
  bindPlus: function () {
    var num = this.data.goodsNum
    num++
    this.setData({ goodsNum: num })
  },
  joinCart: function (e) {
    var that = this;
    if (that.data.seriesTitle == '') {
      wx.showToast({
        title: '未选择型号',
      })
    }
    if (that.data.seriesTitle != '') {
      wx.login({
        success: function (res) {
          // console.log(res)
          that.setData({ jsCode: res.code })
          wx.request({
            url: getApp().globalData.domain + 'api/carts/add',
            data: {
              commodity_name: that.data.name,
              price: that.data.price,
              commodity_id: that.data.id,
              img: that.data.img,
              // user_id:,
              openid: getApp().globalData.openid,
              // token: getApp().globalData.userInfo.token,
              // receiving_address_id: that.data.name,
              commodity_series_id: that.data.seriesId,
              commodity_seriesTitle: that.data.seriesTitle,
              num: that.data.goodsNum,
              jsCode: that.data.jsCode
            },
            method: "POST",
            header: {
              'Content-Type': 'application/json'
            },
            success: function (res) {
              // console.log(res)
              wx.navigateTo({
                url: '../cart/cart',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
          })
        }
      })
    }
  },
  buy:function(){
    var id = this.data.id
    var img = this.data.img
    var goodsNumb = this.data.goodsNum
    var introduce = this.data.introduce
    var name = this.data.name
    var price = this.data.price
    var xseriesId = this.data.seriesId
    var xseriesTitle = this.data.seriesTitle
    if (xseriesTitle == '') {
      wx.showToast({
        title: '未选择型号',
      })
    }
    if (xseriesTitle != '' && xseriesTitle != undefined) {
      wx.navigateTo({
        url: '../order/order?id=' + id + '&img=' + img + '&goodsNum=' + goodsNumb + '&introduce=' + introduce + '&name=' + name + '&price=' + price + '&seriesId=' + xseriesId + '&seriesTitle=' + xseriesTitle,
      })
    }
  }
})